@model List<HDLogCenter.Infrastructure.Models.ViewModels.AlertRuleGroupViewModel>

@{
    ViewData["Title"] = "警告配置管理";
}
<div class="layui-card">
    <div class="layui-card-header">
        <h2>警告配置管理</h2>
        <div class="layui-btn-group">
            <a href="/AlertConfig/Create" class="layui-btn layui-btn-sm">
                <i class="layui-icon layui-icon-add-1"></i> 新增规则
            </a>
        </div>
    </div>
    <div class="layui-card-body">
        @if (Model != null && Model.Any())
        {
            @foreach (var group in Model)
            {
                <div class="layui-card" style="margin-bottom: 15px;">
                    <div class="layui-card-header">
                        <h3>@(group.Project?.AppName ?? group.ProjectCode) (@group.ProjectCode)</h3>
                        <div class="layui-btn-group">
                            <a href="/AlertConfig/Create?projectCode=@group.ProjectCode" class="layui-btn layui-btn-xs">
                                <i class="layui-icon layui-icon-add-1"></i> 为该项目新增规则
                            </a>
                        </div>
                    </div>
                    <div class="layui-card-body">
                        <table class="layui-table" lay-skin="line">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>日志级别</th>
                                    <th>阈值次数</th>
                                    <th>时间窗口(分钟)</th>
                                    <th>通知类型</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var rule in group.Rules)
                                {
                                    <tr>
                                        <td>@rule.Id</td>
                                        <td>
                                            @if (rule.LogLevel == 1)
                                            {
                                                <span class="layui-badge layui-bg-red">Error (1)</span>
                                            }
                                            else if (rule.LogLevel == 2)
                                            {
                                                <span class="layui-badge layui-bg-orange">Warning (2)</span>
                                            }
                                            else if (rule.LogLevel == 3)
                                            {
                                                <span class="layui-badge layui-bg-blue">Information (3)</span>
                                            }
                                            else if (rule.LogLevel == 4)
                                            {
                                                <span class="layui-badge layui-bg-gray">Debug (4)</span>
                                            }
                                            else
                                            {
                                                <span class="layui-badge layui-bg-gray">Unknown (@rule.LogLevel)</span>
                                            }
                                        </td>
                                        <td>@rule.ThresholdCount</td>
                                        <td>@rule.TimeWindow</td>
                                        <td>
                                            @if (rule.NotifyType == "dingtalk")
                                            {
                                                <span class="layui-badge layui-bg-blue">钉钉</span>
                                            }
                                            else if (rule.NotifyType == "email")
                                            {
                                                <span class="layui-badge layui-bg-orange">邮件</span>
                                            }
                                            else if (rule.NotifyType == "sms")
                                            {
                                                <span class="layui-badge layui-bg-red">短信</span>
                                            }
                                            else
                                            {
                                                <span class="layui-badge layui-bg-gray">@rule.NotifyType</span>
                                            }
                                        </td>
                                        <td>
                                            @if (rule.IsEnabled == 1)
                                            {
                                                <span class="layui-badge layui-bg-green">启用</span>
                                            }
                                            else
                                            {
                                                <span class="layui-badge layui-bg-gray">禁用</span>
                                            }
                                        </td>
                                        <td>@rule.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
                                        <td>
                                            <a href="/AlertConfig/Edit/@rule.Id" class="layui-btn layui-btn-xs">编辑</a>
                                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteRule(@rule.Id)">删除</a>
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
            }
        }
        else
        {
            <div class="layui-empty">
                <div class="layui-empty-icon">
                    <i class="layui-icon layui-icon-set"></i>
                </div>
                <div class="layui-empty-text">暂无警告规则配置</div>
                <div class="layui-empty-btn">
                    <a href="/AlertConfig/Create" class="layui-btn">立即创建</a>
                </div>
            </div>
        }
    </div>
</div>

@section Scripts {
<script>
    layui.use(['layer'], function(){
        var layer = layui.layer;

        // 删除警告规则
        window.deleteRule = function(id) {
            layer.confirm('确定要删除这条警告规则吗？', function(index){
                $.post('/AlertConfig/Delete/' + id, function(result){
                    if(result.success){
                        layer.msg('删除成功');
                        setTimeout(function(){
                            location.reload();
                        }, 1000);
                    } else {
                        layer.msg('删除失败: ' + (result.message || '未知错误'));
                    }
                }).fail(function(){
                    layer.msg('删除失败: 网络错误');
                });
                layer.close(index);
            });
        };
    });
</script>
}
